package com.spsoft.system.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.spsoft.core.constants.CacheConstant;
import com.spsoft.core.enums.EnableStatusEnum;
import com.spsoft.core.enums.ResponseEnum;
import com.spsoft.core.model.Result;
import com.spsoft.core.utils.BeanUtils;
import com.spsoft.core.utils.StringUtil;
import com.spsoft.system.entity.DefBizdict;
import com.spsoft.system.mapper.DefBizdictMapper;
import com.spsoft.system.model.vo.DefBizdictVo;
import com.spsoft.system.service.DefBizdictService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spsoft.vms.redis.RedisService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 数据字典类型和类型项试图 服务实现类
 * </p>
 *
 * @author dlchen
 * @since 2023-11-07
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class DefBizdictServiceImpl extends ServiceImpl<DefBizdictMapper, DefBizdict> implements DefBizdictService {


    private final RedisService redisService;

    /**
     * 启动加载数据字典到缓存
     */
    @PostConstruct
    public void init() {
        log.info("************init  初始化【数据字典】到缓存开始*************");
        this.updateDictCache();
        log.info("************init  初始化【数据字典】到缓存结束*************");
    }

    /**
     * 更新dict缓存
     *
     * @return boolean
     */
    @Override
    public boolean updateDictCache() {
        try {
            List<DefBizdict> defBizdicts = this.getBaseMapper().selectList(Wrappers.<DefBizdict>query().lambda()
                    .eq(DefBizdict::getQyzt, EnableStatusEnum.Enable.getCode())
                    .orderByAsc(DefBizdict::getLxdm, DefBizdict::getPlxh)
            );
            //defBizdicts 转换成以lxdm为key的map value为list
            Map<String, List<DefBizdict>> listMap = defBizdicts.stream().collect(Collectors.groupingBy(DefBizdict::getLxdm));
            redisService.setCacheMap(CacheConstant.SYS_CACHE_DICT, listMap);
            log.info("************    初始化【数据字典】到缓存成功,缓存数据：【{}】*************", listMap.size());
            return true;
        } catch (Exception e) {
            log.error("************   初始化【数据字典】到缓存异常{}*************", e.getMessage());
            return false;
        }
    }

    @Override
    public Result<List<DefBizdictVo>> getDefBIzdictByLxdm(String lxdm) {
        if (StringUtil.isEmpty(lxdm)) {
            Result<List<DefBizdictVo>> r = new Result<List<DefBizdictVo>>();
            r.setCode(ResponseEnum.PARAM_VALIDATED_ERROR.getCode());
            r.setMessage("类型代码不能为空");
            return r;
        }

        List<DefBizdict> cacheMapValue = redisService.getCacheMapValue(CacheConstant.SYS_CACHE_DICT, lxdm);
        if (cacheMapValue == null) {
            cacheMapValue = this.getBaseMapper().selectList(Wrappers.<DefBizdict>query().lambda()
                    .eq(DefBizdict::getQyzt, EnableStatusEnum.Enable.getCode())
                    .eq(DefBizdict::getLxdm, lxdm)
                    .orderByAsc(DefBizdict::getLxdm, DefBizdict::getPlxh)
            );
        }
        if (cacheMapValue == null || cacheMapValue.size() == 0) {
            return Result.OK();
        }
        List<DefBizdictVo> defBizdictVos = BeanUtils.copyListProperties(cacheMapValue, DefBizdictVo::new);
        return Result.OK(defBizdictVos);
    }

    @Override
    public Map<String, String> getDefBIzdictNameByLxdm(String lxdm) {
        if (StringUtil.isNotEmpty(lxdm)) {
            List<DefBizdict> cacheMapValue = redisService.getCacheMapValue(CacheConstant.SYS_CACHE_DICT, lxdm);
            if (cacheMapValue == null) {
                cacheMapValue = this.getBaseMapper().selectList(Wrappers.<DefBizdict>query().lambda()
                        .eq(DefBizdict::getQyzt, EnableStatusEnum.Enable.getCode())
                        .eq(DefBizdict::getLxdm, lxdm)
                        .orderByAsc(DefBizdict::getLxdm, DefBizdict::getPlxh)
                );
            }
            Map<String, String> re = cacheMapValue.stream().collect(Collectors.toMap(DefBizdict::getLxxdm, DefBizdict::getLxxmc));

            return re;
        }
        return null;
    }

    public String getDefBizDictValue(String lxdm, String key) {
        Map<String, String> map = getDefBIzdictNameByLxdm(lxdm);
        return map.get(key);
    }


}
